package com.hh.game.service.gateway.config;

import cn.hutool.core.util.StrUtil;
import com.hh.game.common.base.cache.redis.RedisResource;
import com.hh.game.common.base.config.RedisConfig;
import com.hh.game.service.gateway.filter.ip.IpConfig;
import com.hh.game.service.gateway.filter.ip.RateLimitConfig;
import com.hh.game.service.gateway.filter.request.SynRequestConfig;
import com.hh.game.service.gateway.filter.security.SecurityConfig;
import com.hh.game.service.gateway.filter.statistics.StatisticsConfig;
import com.hh.game.service.gateway.webscoket.config.WebSocketServerConfig;
import com.hh.game.service.gateway.webscoket.config.WebSocketServerDefaultConfig;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AutoConfiguration {
    private RateLimitConfig rateLimit;
    private WebSocketServerConfig webSocket;
    private IpConfig ip;
    private SynRequestConfig synRequest;
    private SecurityConfig security;
    private RedisConfig redis;
    private StatisticsConfig statistics;


    @Bean
    public StatisticsConfig statisticsConfig(){
        return statistics;
    }

    @Bean
    public SecurityConfig securityConfig(){
        return security;
    }

    @Bean
    public SynRequestConfig synRequestConfig(){
        return synRequest;
    }

    @Bean
    public IpConfig ipConfig(){
        return ip;
    }

//    @Bean
//    @ConfigurationProperties(prefix = "app.redis")
//    public RedisConfig redisConfig(){
//        return new RedisConfig();
//    }

    @Bean
    @ConfigurationProperties(prefix = "redis")
    public RedisResource redisResource(){
        return new RedisResource(redis.redissonClient() ,redis.getPref());
    }

    @Bean
    public RateLimitConfig rateLimitConfig(){
        return rateLimit;
    }

    @Bean
    public WebSocketServerConfig websocketServerConfig(){
        if(webSocket.getPort() <= 0 ){
            webSocket.setPort(WebSocketServerDefaultConfig.port);
        }
        if(StrUtil.isBlank(webSocket.getPath())){
            webSocket.setPath(WebSocketServerDefaultConfig.path);
        }else if(!webSocket.getPath().startsWith("/")){
            webSocket.setPath("/" + webSocket.getPath());
        }
        if(webSocket.getMaxConnectSize() <= 0){
            webSocket.setMaxConnectSize(WebSocketServerDefaultConfig.maxConnectSize);
        }

        if(webSocket.getWorkLoopGroupThreads() <= 0){
            webSocket.setWorkLoopGroupThreads(WebSocketServerDefaultConfig.workLoopGroupThreads);
        }
        if(webSocket.getWorkPoolQueueSize() <= 0){
            webSocket.setWorkPoolQueueSize(WebSocketServerDefaultConfig.workPoolQueueSize);
        }

        if(webSocket.getReaderIdleTimeSeconds() <= 0){
            webSocket.setReaderIdleTimeSeconds(WebSocketServerDefaultConfig.readerIdleTimeSeconds);
        }
        if(webSocket.getWriterIdleTimeSeconds() <= 0){
            webSocket.setWriterIdleTimeSeconds(WebSocketServerDefaultConfig.writerIdleTimeSeconds);
        }
        if(webSocket.getAllIdleTimeSeconds() <= 0){
            webSocket.setAllIdleTimeSeconds(WebSocketServerDefaultConfig.allIdleTimeSeconds);
        }

        if(webSocket.getWorkThreadSize() <= 0){
            webSocket.setWorkThreadSize(WebSocketServerDefaultConfig.workThreadSize);
        }

        if(webSocket.getWorkQueueSize() <= 0){
            webSocket.setWorkQueueSize(WebSocketServerDefaultConfig.workQueueSize);
        }

        if(!StrUtil.isBlank(webSocket.getSslCertificate())
                && !StrUtil.isBlank(webSocket.getSslCertificateKey())){
            webSocket.setSSL(true);
        }
        return webSocket;
    }


    public void setWebSocket(WebSocketServerConfig webSocket) {
        this.webSocket = webSocket;
    }


    public void setRateLimit(RateLimitConfig rateLimit) {
        this.rateLimit = rateLimit;
    }

    public void setIp(IpConfig ip) {
        this.ip = ip;
    }

    public void setSynRequest(SynRequestConfig synRequest) {
        this.synRequest = synRequest;
    }

    public void setSecurity(SecurityConfig security) {
        this.security = security;
    }

    public void setRedis(RedisConfig redis) {
        this.redis = redis;
    }

    public void setStatistics(StatisticsConfig statistics) {
        this.statistics = statistics;
    }
}
